package com.zqkey.rescuecommand.screen;

import java.util.ArrayList;
import java.util.List;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector3;
import com.zqkey.rescuecommand.Assets;
import com.zqkey.rescuecommand.Game;
import com.zqkey.rescuecommand.OverlapTester;
import com.zqkey.rescuecommand.Settings;
import com.zqkey.rescuecommand.entity.Cabinet;
import com.zqkey.rescuecommand.entity.Ground;

public class GameScreen extends Screen {
	public static final int cabinetWidth = 60;
	public static final int cabinetHeight = 60;
	
	SpriteBatch batcher;
	List<Cabinet> cabinetsMain;
	List<Cabinet> cabinetsFun1;
	List<Cabinet> cabinetsFun2;
	List<Cabinet> cabinetsCommand;
	Vector3 touchPoint;
	
	// 0:没有拿起任何“命令”	1：正拿着一个“命令”
	byte touchStatus;
	// 抓起的命令
	Cabinet grabCabinet;
	
	Cabinet start;
	Cabinet reset;
	Rectangle music_button;
	
	Ground[][] grounds = new Ground[4][13];
	
	public GameScreen(Game game) {
		super(game);
		batcher = new SpriteBatch();
		touchPoint = new Vector3();
		cabinetsMain = new ArrayList<Cabinet>();
		cabinetsFun1 = new ArrayList<Cabinet>();
		cabinetsFun2 = new ArrayList<Cabinet>();
		cabinetsCommand = new ArrayList<Cabinet>();
		
		grabCabinet = new Cabinet(new Rectangle(0, 0, 0, 0), Assets.cabinet);

		
		for(int i=1; i>=0; i--) {
			for(int j=0; j<5; j++) {
				Rectangle r = new Rectangle((5 + cabinetWidth)*j+10, (5 + cabinetHeight)*i+10, cabinetWidth, cabinetHeight);
				Cabinet c = new Cabinet(r, Assets.cabinet);
				cabinetsMain.add(c);
			}
		}
		for(int i=1; i>=0; i--) {
			for(int j=0; j<3; j++) {
				Rectangle r = new Rectangle((5 + cabinetWidth)*j+370, (5 + cabinetHeight)*i+10, cabinetWidth, cabinetHeight);
				Cabinet c = new Cabinet(r, Assets.cabinet);
				cabinetsFun1.add(c);
			}
		}
		for(int i=1; i>=0; i--) {
			for(int j=0; j<3; j++) {
				Rectangle r = new Rectangle((5 + cabinetWidth)*j+600, (5 + cabinetHeight)*i+10, cabinetWidth, cabinetHeight);
				Cabinet c = new Cabinet(r, Assets.cabinet);
				cabinetsFun2.add(c);
			}
		}
		
		cabinetsCommand.add(new Cabinet(new Rectangle((5 + cabinetWidth)*0+10, (5 + cabinetHeight)*2+10 + 30, cabinetWidth, cabinetHeight), Assets.commandForward, 1));
		cabinetsCommand.add(new Cabinet(new Rectangle((5 + cabinetWidth)*1+10, (5 + cabinetHeight)*2+10 + 30, cabinetWidth, cabinetHeight), Assets.commandClockwise, 2));
		cabinetsCommand.add(new Cabinet(new Rectangle((5 + cabinetWidth)*2+10, (5 + cabinetHeight)*2+10 + 30, cabinetWidth, cabinetHeight), Assets.commandAnticlockwise, 3));
		cabinetsCommand.add(new Cabinet(new Rectangle((5 + cabinetWidth)*3+10, (5 + cabinetHeight)*2+10 + 30, cabinetWidth, cabinetHeight), Assets.commandJump, 4));
		cabinetsCommand.add(new Cabinet(new Rectangle((5 + cabinetWidth)*4+10, (5 + cabinetHeight)*2+10 + 30, cabinetWidth, cabinetHeight), Assets.commandKey, 5));
		cabinetsCommand.add(new Cabinet(new Rectangle((5 + cabinetWidth)*5+10, (5 + cabinetHeight)*2+10 + 30, cabinetWidth, cabinetHeight), Assets.commandFun1, 6));
		cabinetsCommand.add(new Cabinet(new Rectangle((5 + cabinetWidth)*6+10, (5 + cabinetHeight)*2+10 + 30, cabinetWidth, cabinetHeight), Assets.commandFun2, 7));
		
		start = new Cabinet(new Rectangle(580, (5 + cabinetHeight)*2+10 + 30, cabinetWidth, cabinetHeight), Assets.cabinet);
		reset = new Cabinet(new Rectangle(650, (5 + cabinetHeight)*2+10 + 30, cabinetWidth, cabinetHeight), Assets.cabinet);
		music_button = new Rectangle(10, 420, 50, 50);
		
		for(int i=0; i<4; i++) {
			for(int j=0; j<13; j++) {
				grounds[i][j] = new Ground(Assets.ground, 0);
			}
		}
	}

	@Override
	public void update(float deltaTime) {
		// 当前没有抓着命令
		if(touchStatus == 0) {
			if (Gdx.input.justTouched()) {
				touchPoint.set(Gdx.input.getX(), Settings.gameHeight - Gdx.input.getY(), 0);
				
				for(Cabinet c : cabinetsCommand) {
					if(OverlapTester.pointInRectangle(c.bounds, touchPoint.x, touchPoint.y)) {
						grabCabinet.texture = c.texture;
						grabCabinet.bounds = new Rectangle(touchPoint.x - cabinetWidth/2, touchPoint.y - cabinetHeight/2, cabinetWidth, cabinetHeight);
						grabCabinet.command = c.command;
						touchStatus = 1;
						return;
					}
				}
				for(Cabinet c : cabinetsMain) {
					if(OverlapTester.pointInRectangle(c.bounds, touchPoint.x, touchPoint.y) && c.command > 0) {
						grabCabinet.texture = c.texture;
						grabCabinet.bounds = new Rectangle(touchPoint.x - cabinetWidth/2, touchPoint.y - cabinetHeight/2, cabinetWidth, cabinetHeight);
						grabCabinet.command = c.command;
						// 格子中的命令还原
						c.texture = Assets.cabinet;
						c.command = 0;
						touchStatus = 1;
						return;
					}
				}
				for(Cabinet c : cabinetsFun1) {
					if(OverlapTester.pointInRectangle(c.bounds, touchPoint.x, touchPoint.y) && c.command > 0) {
						grabCabinet.texture = c.texture;
						grabCabinet.bounds = new Rectangle(touchPoint.x - cabinetWidth/2, touchPoint.y - cabinetHeight/2, cabinetWidth, cabinetHeight);
						grabCabinet.command = c.command;
						// 格子中的命令还原
						c.texture = Assets.cabinet;
						c.command = 0;
						touchStatus = 1;
						return;
					}
				}
				for(Cabinet c : cabinetsFun2) {
					if(OverlapTester.pointInRectangle(c.bounds, touchPoint.x, touchPoint.y) && c.command > 0) {
						grabCabinet.texture = c.texture;
						grabCabinet.bounds = new Rectangle(touchPoint.x - cabinetWidth/2, touchPoint.y - cabinetHeight/2, cabinetWidth, cabinetHeight);
						grabCabinet.command = c.command;
						// 格子中的命令还原
						c.texture = Assets.cabinet;
						c.command = 0;
						touchStatus = 1;
						return;
					}
				}
				
				// 重置事件
				if(OverlapTester.pointInRectangle(reset.bounds, touchPoint.x, touchPoint.y)) {
					for(Cabinet c : cabinetsMain) {
						c.texture = Assets.cabinet;
						c.command = 0;
					}
					for(Cabinet c : cabinetsFun1) {
						c.texture = Assets.cabinet;
						c.command = 0;
					}
					for(Cabinet c : cabinetsFun2) {
						c.texture = Assets.cabinet;
						c.command = 0;
					}
				}
				
				// 开始事件
				if(OverlapTester.pointInRectangle(start.bounds, touchPoint.x, touchPoint.y)) {
					int i = 1;
					for(Cabinet c : cabinetsMain) {
//						System.out.println(i + ":" + c.command);
						i++;
					}
				}
				
				// 背景音乐控制
				if(OverlapTester.pointInRectangle(music_button, touchPoint.x, touchPoint.y)) {
					if(Settings.soundEnabled) {
						Settings.soundEnabled = false;
						Assets.pauseMusic(Assets.music);
					} else {
						Settings.soundEnabled = true;
						Assets.playMusic(Assets.music);
					}
				}
			}
		} else if(touchStatus == 1) {
			if (Gdx.input.isTouched()) {
				touchPoint.set(Gdx.input.getX(), Settings.gameHeight - Gdx.input.getY(), 0);
				grabCabinet.bounds.x = touchPoint.x - cabinetWidth/2;
				grabCabinet.bounds.y = touchPoint.y - cabinetHeight/2;
			} else {
				touchStatus = 0;
				for(Cabinet c : cabinetsMain) {
					if(OverlapTester.pointInRectangle(c.bounds, touchPoint.x, touchPoint.y)) {
						c.texture = grabCabinet.texture;
						c.command = grabCabinet.command;
						return;
					}
				}
				for(Cabinet c : cabinetsFun1) {
					if(OverlapTester.pointInRectangle(c.bounds, touchPoint.x, touchPoint.y)) {
						c.texture = grabCabinet.texture;
						c.command = grabCabinet.command;
						return;
					}
				}
				for(Cabinet c : cabinetsFun2) {
					if(OverlapTester.pointInRectangle(c.bounds, touchPoint.x, touchPoint.y)) {
						c.texture = grabCabinet.texture;
						c.command = grabCabinet.command;
						return;
					}
				}
			}
		}
	}

	@Override
	public void present(float deltaTime) {
		batcher.begin();
		batcher.draw(Assets.backgroundRegion, 0, 0, Settings.gameWidth, Settings.gameHeight);
		for(int i=0; i<cabinetsMain.size(); i++) {
			batcher.draw(cabinetsMain.get(i).texture, cabinetsMain.get(i).bounds.x, cabinetsMain.get(i).bounds.y, cabinetsMain.get(i).bounds.width, cabinetsMain.get(i).bounds.height);
		}
		for(int i=0; i<cabinetsFun1.size(); i++) {
			batcher.draw(cabinetsFun1.get(i).texture, cabinetsFun1.get(i).bounds.x, cabinetsFun1.get(i).bounds.y, cabinetsFun1.get(i).bounds.width, cabinetsFun1.get(i).bounds.height);
		}
		for(int i=0; i<cabinetsFun2.size(); i++) {
			batcher.draw(cabinetsFun2.get(i).texture, cabinetsFun2.get(i).bounds.x, cabinetsFun2.get(i).bounds.y, cabinetsFun2.get(i).bounds.width, cabinetsFun2.get(i).bounds.height);
		}
		for(int i=0; i<cabinetsCommand.size(); i++) {
			batcher.draw(cabinetsCommand.get(i).texture, cabinetsCommand.get(i).bounds.x, cabinetsCommand.get(i).bounds.y, cabinetsCommand.get(i).bounds.width, cabinetsCommand.get(i).bounds.height);
		}
		
		// 绘制【开始】与【重置】
		batcher.draw(start.texture, start.bounds.x, start.bounds.y, start.bounds.width, start.bounds.height);
		batcher.draw(reset.texture, reset.bounds.x, reset.bounds.y, reset.bounds.width, reset.bounds.height);
		// 背景音乐按钮
		batcher.draw(Assets.musicImg, music_button.x, music_button.y, music_button.width, music_button.height);
		
		for(int i=0; i<13; i++) {
			for(int j=0; j<4; j++) {
				if(i%2==0) {
					batcher.draw(grounds[j][i].texture, 80 + 50*i, 245 + 50*j, 100, 50);
				} else {
					batcher.draw(grounds[j][i].texture, 80 + 50*i, 245 + 25 + 50*j, 100, 50);
				}
			}
		}
//		
//		batcher.draw(Assets.ground, 200+49, 300+26, 100, 50);
//		batcher.draw(Assets.ground, 200+49, 300-26, 100, 50);
		
		// 绘制别抓起的命令
		if(touchStatus == 1) {
			batcher.draw(grabCabinet.texture, grabCabinet.bounds.x, grabCabinet.bounds.y, grabCabinet.bounds.width, grabCabinet.bounds.height);
		}
		
		batcher.end();
	}

	@Override
	public void pause() {
		Assets.pauseMusic(Assets.music);	
	}

	@Override
	public void resume() {
		Assets.playMusic(Assets.music);
	}

	@Override
	public void dispose() {
		// TODO Auto-generated method stub
	}

}
